import Taro from '@tarojs/taro'
import { CustomRequest } from './request';
interface IUploadConfig {
    url: string;
    fileName: string
}

const isFromCloud = (url: string) => {
    const reg = /^https.*/;
    if (reg.test(url)) {
        return true;
    } else {
        return false;
    }
};

export default class ImageInfo {
    static Upload(url: string, filePath: string, dir: string) {
        return new Promise((resolve, reject) => {
            console.log("filepath", filePath);
            if (isFromCloud(filePath)) {
                resolve(filePath);
                return
            }
            Taro.uploadFile({
                url: `${url}`,
                filePath,
                name: "file",
                formData: {
                    dir: 'd'
                },
                header: {
                    "Content-Type": "multipart/form-data",
                    "token": Taro.getStorageSync("token")
                },
                success: (res: any) => {
                    const jsonData = JSON.parse(res.data);
                    console.log('jsonData:', jsonData)
                    resolve(jsonData.data.id);
                },
                fail: reject
            });
        });
    }

    static UploadFile(url: string, filePath: any, _dir?: string) {
        return new Promise((resolve, reject) => {
            Taro.uploadFile({
                url: `${url}`,
                filePath,
                name: "file",
                formData: {
                    // dir: 'd'
                },
                header: {
                    // "Content-Type": "multipart/form-data",
                    // "Content-Type": "application/x-www-form-urlencoded",
                    "token": Taro.getStorageSync("token")
                },
                success: (res: any) => {
                    const jsonData = JSON.parse(res.data);
                    console.log('jsonData:', jsonData)
                    resolve(jsonData.data.id);
                },
                fail: reject
            });
        });
    }

    // canvas支持
    static compressedFinalPics = async (tempPath: string[], uploadConfig: IUploadConfig, originFiles?: any) => {
        /**
         * if has originFiles 用files
         */
        // if (originFiles) {
        //     let uploadFilesPromsie: Promise<any>[] = [];
        //     originFiles.forEach(ele => {
        //         uploadFilesPromsie.push(ImageInfo.UploadFile(uploadConfig.url, ele, uploadConfig.fileName));
        //     });
        //     await Promise.all(uploadFilesPromsie)
        //         .then((res: string[]) => {
        //             res.forEach(element => {
        //                 files.push(element);
        //             });
        //         })
        //         .finally(() => {
        //             return files;
        //         });
        // }

        let files: string[] = [];
        let uploadFiles: Promise<any>[] = [];
        tempPath.forEach(ele => {
            uploadFiles.push(ImageInfo.UploadFile(uploadConfig.url, ele, uploadConfig.fileName));
        });
        await Promise.all(uploadFiles)
            .then((res: string[]) => {
                res.forEach(element => {
                    files.push(element);
                });
            })
            .finally(() => {
                return files;
            });
        return files;
    };






}

